/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package beans;

import beans.exceptions.NonexistentEntityException;
import entities.Cart;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import entities.Client;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ApplicationScoped;



@ManagedBean(name="cartDao")
@ApplicationScoped
public class CartDAO {

    public CartDAO() {
        emf = Persistence.createEntityManagerFactory("BookstorePU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Cart cart) {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Client clientId = cart.getClientId();
            if (clientId != null) {
                clientId = em.getReference(clientId.getClass(), clientId.getId());
                cart.setClientId(clientId);
            }
            em.persist(cart);
            if (clientId != null) {
                Cart oldCartOfClientId = clientId.getCart();
                if (oldCartOfClientId != null) {
                    oldCartOfClientId.setClientId(null);
                    oldCartOfClientId = em.merge(oldCartOfClientId);
                }
                clientId.setCart(cart);
                clientId = em.merge(clientId);
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Cart cart) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Cart persistentCart = em.find(Cart.class, cart.getId());
            Client clientIdOld = persistentCart.getClientId();
            Client clientIdNew = cart.getClientId();
            if (clientIdNew != null) {
                clientIdNew = em.getReference(clientIdNew.getClass(), clientIdNew.getId());
                cart.setClientId(clientIdNew);
            }
            cart = em.merge(cart);
            if (clientIdOld != null && !clientIdOld.equals(clientIdNew)) {
                clientIdOld.setCart(null);
                clientIdOld = em.merge(clientIdOld);
            }
            if (clientIdNew != null && !clientIdNew.equals(clientIdOld)) {
                Cart oldCartOfClientId = clientIdNew.getCart();
                if (oldCartOfClientId != null) {
                    oldCartOfClientId.setClientId(null);
                    oldCartOfClientId = em.merge(oldCartOfClientId);
                }
                clientIdNew.setCart(cart);
                clientIdNew = em.merge(clientIdNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Long id = cart.getId();
                if (findCart(id) == null) {
                    throw new NonexistentEntityException("The cart with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Long id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Cart cart;
            try {
                cart = em.getReference(Cart.class, id);
                cart.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The cart with id " + id + " no longer exists.", enfe);
            }
            Client clientId = cart.getClientId();
            if (clientId != null) {
                clientId.setCart(null);
                clientId = em.merge(clientId);
            }
            em.remove(cart);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Cart> getAllCarts() {
        return getAllCarts(true, -1, -1);
    }

    public List<Cart> getAllCarts(int maxResults, int firstResult) {
        return getAllCarts(false, maxResults, firstResult);
    }

    private List<Cart> getAllCarts(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createNamedQuery("getAllCarts");
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Cart findCart(Long id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Cart.class, id);
        } finally {
            em.close();
        }
    }

    public int getCartCount() {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select count(o) from Cart as o");
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
